import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:news_flutter/models/news_model.dart';
import 'package:news_flutter/pages/webview_page.dart';
import 'package:news_flutter/utils/my_navigator.dart';
import 'package:news_flutter/utils/precompile.dart';
import 'package:news_flutter/widgets/custom/cache_image_widget.dart';

class NewsItemWidget extends StatefulWidget {
  final NewsModel newsModel;

  const NewsItemWidget({Key key, this.newsModel}) : super(key: key);
  @override
  _NewsItemWidgetState createState() => _NewsItemWidgetState();
}

class _NewsItemWidgetState extends State<NewsItemWidget> {
  @override
  Widget build(BuildContext context) {
    var coverUrl = widget.newsModel.picUrl;
    bool hasCover = (coverUrl!=null&&coverUrl!="");
    return InkWell(
      onTap: (){
        //跳转到详情页面
        WebViewPage webViewPage = WebViewPage(url: widget.newsModel.url??"",title: widget.newsModel.title??"",);
        MyNavigator.push(context, webViewPage);
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          children: <Widget>[
          hasCover?_titleAndCoverWidget():_singleTitleWidget(),
            //分割线
            Divider(
              height: 0.5,
              color: MyColor.DIVER_LINE_GRAY,
            ),
          ],
        ),
      ),
    );
  }
  //只有标题，不含封面图
  Widget _singleTitleWidget(){
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        //标题
        Container(
          padding: EdgeInsets.symmetric(vertical:5),
          alignment: Alignment.topLeft,
          child: Text(widget.newsModel.title??"",style: TextStyle(fontSize: 15),maxLines: 3,),
        ),
        //标签、跟帖数
        Container(
          padding: EdgeInsets.only(bottom: 5),
          child: Row(
            children: <Widget>[
              Container(
                child: Text(widget.newsModel.source??"",style: TextStyle(color: MyColor.TEXT_GRAY),),
              ),
              Expanded(
                child: Container(
                  margin: EdgeInsets.only(left: 5),
                  child: Text(widget.newsModel.ctime??"",style: TextStyle(color: MyColor.TEXT_GRAY),),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
  //包含标题+封面图
Widget _titleAndCoverWidget(){
  return IntrinsicHeight(
    child: Row(
      children: <Widget>[
        //左边文字
        Expanded(
          child: _singleTitleWidget(),
        ),
        //右边封面图
        Container(
          padding: EdgeInsets.symmetric(vertical: 5),
          margin: EdgeInsets.only(left: 5),
          width: 130,
          height: 80,
          child: MyCacheImage(
            url: widget.newsModel.picUrl,
          ),
        ),
      ],
    ),
  );
  }
}
